package com.chenqq.qsocket.fractory;

import android.util.Log;

import com.chenqq.qsocket.configuration.MinaConfigInfo;
import com.chenqq.qsocket.message.KeepAliveMessage;
import com.chenqq.qsocket.message.SocketMessage;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;

/**
 * 发送端心跳包
 */
public class HeartBeatMessageFactory implements KeepAliveMessageFactory {
    private KeepAliveMessage keepAliveSend = new KeepAliveMessage(MinaConfigInfo.getInStance().HeardSend);//客户端请求包
    private KeepAliveMessage keepAliveAccept = new KeepAliveMessage(MinaConfigInfo.getInStance().HeardAccept);//服务端回馈包

    //判断接收到的消息是不是心跳请求包
    @Override
    public boolean isRequest(IoSession ioSession, Object o) {
        Log.e("isRequest","");
        if (o instanceof String){
            Log.e("isRequest",""+o.equals(keepAliveSend.getJSONBody()));
           return o.equals(keepAliveSend.getJSONBody());
        }

        return false;
    }

    //判断接收到的消息是不是心跳回复包
    @Override
    public boolean isResponse(IoSession ioSession, Object o) {
        Log.e("isResponse","");
        if (o instanceof SocketMessage){
            Log.e("isResponse",""+keepAliveAccept.equals(o));
            return o.equals(keepAliveSend.getJSONBody());
        }
        return false;
    }

    //获取心跳请求包
    @Override
    public Object getRequest(IoSession ioSession) {
        Log.e("getRequest",""+keepAliveSend.getJSONBody());
        return keepAliveSend.getJSONBody();
    }

    //获取心跳回复包
    @Override
    public Object getResponse(IoSession ioSession, Object o) {
        Log.e("getRequest",""+keepAliveAccept.getJSONBody());
        return keepAliveAccept.getJSONBody();
    }
}
